package com.modifiertech.lineage.modifier;

import com.modifiertech.lineage.LineageRegistries;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.api.PlayerDataSkill;
import com.modifiertech.lineage.event.PlayerDataHandler;
import com.modifiertech.lineage.skills.StrongAsBull;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.MathHelper;

import java.util.Optional;
import java.util.UUID;

/**
 * ClassName: HealthModifier <br/>
 * Description: <br/>
 * date: 2020/8/23 18:30<br/>
 *
 * @author Administrator<br />
 * @since JDK 1.8
 */
public class HealthModifier {
    public static final int modifier_add = 0;
    public static final int multiply_base = 1;
    public static final int modifier_total = 2;


    /**获取指定修饰类型的血量修饰
     *
     * @param entity
     * @param type
     * @return
     */
    public static double getHealthModifier(EntityLivingBase entity,Type type) {
        IAttributeInstance attr = entity.getAttributeMap().getAttributeInstance(SharedMonsterAttributes.MAX_HEALTH);
        //noinspection ConstantConditions
        if (attr == null) {
            return 0;
        }
        AttributeModifier mod = attr.getModifier(type.getId());
        return mod != null ? mod.getAmount() : 0;
    }

    public static double getAllModHealthModifier(EntityLivingBase entity) {
        IAttributeInstance attr = entity.getAttributeMap().getAttributeInstance(SharedMonsterAttributes.MAX_HEALTH);
        //noinspection ConstantConditions
        if (attr == null) {
            return 0;
        }
        double d=0d;
        for (Type value : Type.values()) {
            AttributeModifier mod = attr.getModifier(value.getId());
            d+=mod != null ? mod.getAmount() : 0;
        }
        return d;
    }

    /**
     *
     * @param entity
     */
    public static void clearAllModHealthModifier(EntityLivingBase entity){
        IAttributeInstance attr = entity.getAttributeMap().getAttributeInstance(SharedMonsterAttributes.MAX_HEALTH);
        for (Type value : Type.values()) {
            AttributeModifier mod = attr.getModifier(value.getId());
            if (null!=mod){
                synchronized (mod){
                    attr.removeModifier(value.getId());
                }
            }
        }
    }

    /**设定指定修饰类型的血量修饰
     *
     * @param entity
     * @param type
     * @param amount
     */
    public static void setModifier(EntityLivingBase entity,Type type,int amount){
        IAttributeInstance attr = entity.getAttributeMap().getAttributeInstance(SharedMonsterAttributes.MAX_HEALTH);
        AttributeModifier mod = attr.getModifier(type.getId());
        synchronized (attr){
            if (null!=mod){
                attr.removeModifier(type.getId());
            }
            attr.applyModifier(new AttributeModifier(type.getId(),type.getName(),amount,modifier_add));
        }
    }
    /**
     * @param entity
     * @param data
     */
    public static void sync(EntityPlayer entity,PlayerData data){
        clearAllModHealthModifier(entity);
        revert_MODIFIER_STRONGASBULL(entity,data);
        revert_MODIFIER_ATTR();
        revert_MODIFIER_EQUIP();
        checkOverFlow(entity);
    }

    private static void checkOverFlow(EntityPlayer entity) {
        int health = MathHelper.ceil(entity.getHealth());
        IAttributeInstance attrMaxHealth = entity.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH);
        int healthMax = MathHelper.ceil(attrMaxHealth.getAttributeValue());
        if (health>healthMax){
            entity.setHealth(healthMax);
        }
    }




    public static void syncWithFullHealth(EntityPlayer entity,PlayerData data){
        sync(entity,data);
        fullFillHealth(entity);
    }


    public static void fullFillHealth(EntityPlayer entity){
        synchronized (entity){
            entity.setHealth(entity.getMaxHealth());
        }
    }


    private static void revert_MODIFIER_STRONGASBULL(EntityPlayer entity,PlayerData data){
        Optional<PlayerDataSkill>
                playerDataSkill= data.getPlayerDataSkillByName(StrongAsBull.NAME);
        if (!playerDataSkill.isPresent())return;
        StrongAsBull.revertHealthModifier(entity,playerDataSkill.get().getSkillLevel());
    }
    private static void revert_MODIFIER_ATTR(){

    }
    private static void revert_MODIFIER_EQUIP(){

    }
















    public static enum Type{
        MODIFIER_STRONGASBULL(UUID.fromString("c58f1434-3ef6-4839-b464-25207754d09e"),"com.modifiertech.lineage.modifier.HealthModifier.StrongAsBull"),
        MODIFIER_ATTR(UUID.fromString("04b0d0db-599d-47c0-b8a9-646b29ba6b70"),"com.modifiertech.lineage.modifier.HealthModifier.attribute"),
        MODIFIER_EQUIP(UUID.fromString("b0b869d2-1abf-4b08-9409-7af2901b06d8"),"com.modifiertech.lineage.modifier.HealthModifier.equipment");

        private UUID id;
        private String name;

        Type(UUID id,String name) {
            this.id = id;
            this.name=name;
        }

        public UUID getId() {
            return id;
        }

        public void setId(UUID id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
